package com.lefevre.cms.web.action.theme;

import com.lefevre.cms.bean.PageForm;
import com.lefevre.cms.bean.RequestResult;
import com.lefevre.cms.bean.ResultCode;
import com.lefevre.cms.bean.theme.Themes;
import com.lefevre.cms.service.theme.ThemeService;
import com.lefevre.cms.utils.FileUtil;
import com.lefevre.cms.utils.JsonUtils;
import com.lefevre.cms.utils.PathUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

/**
 * 资源管理 分页显示
 */
@Controller
public class ResourceAction {
    @Resource
    ThemeService themeService;

    @ResponseBody
    @RequestMapping("/control/resource/list")
    public String execute(@RequestParam("dirName") String dirName, PageForm pageForm, ModelMap model) {

        Map<String, String> error = new HashMap<>();
        if (dirName != null && !"".equals(dirName.trim())) {
            Themes themes = themeService.findTemplateByDirName(dirName);
            return JsonUtils.toJSONString(new RequestResult(ResultCode.SUCCESS, themes));
        } else {
            error.put("dirName", "目录名称不能为空");
        }
        return JsonUtils.toJSONString(new RequestResult(ResultCode.FAILURE, error));
    }

    /**
     * 查询资源子节点
     *
     * @param dirName
     * @param parentId 父Id
     * @param model    @RequestParam("dirName") String dirName,
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/control/resource/query", method = RequestMethod.GET)
    public String queryChildNode(String dirName, String parentId, ModelMap model,
                                 HttpServletRequest request) {

        Map<String, String> error = new HashMap<>();
        if (dirName != null && !"".equals(dirName.trim())) {
            List<com.lefevre.cms.bean.theme.Resource> resourceList = new ArrayList<com.lefevre.cms.bean.theme.Resource>();

            String path = "";
            if (PathUtil.isStartupFromJar()) {//jar启动
                path = PathUtil.autoRootPath() + File.separator + "common" + File.separator + FileUtil.toRelativePath(dirName) + (parentId == null || "".equals(parentId.trim()) ? "" : File.separator + FileUtil.toRelativePath(FileUtil.toSystemPath(parentId)));
            } else {//IDE启动
                path = PathUtil.autoRootPath() + File.separator + "static" + File.separator + "common" + File.separator + FileUtil.toRelativePath(dirName) + (parentId == null || "".equals(parentId.trim()) ? "" : File.separator + FileUtil.toRelativePath(FileUtil.toSystemPath(parentId)));

            }

            File dir = new File(path);
            if (dir.isDirectory()) {

                File[] fs = dir.listFiles();
                for (File file : fs) {
                    com.lefevre.cms.bean.theme.Resource resource = new com.lefevre.cms.bean.theme.Resource();
                    if (parentId == null || "".equals(parentId.trim())) {
                        resource.setId(file.getName());
                    } else {
                        resource.setId(parentId + "/" + file.getName());
                    }
                    resource.setLastModified(new Date(file.lastModified()));
                    if (file.isDirectory()) {//是目录
                        resource.setLeaf(false);//不是叶子节点
                    } else {
                        resource.setLeaf(true);//是叶子节点
                    }
                    resource.setName(file.getName());
                    resourceList.add(resource);
                }
                return JsonUtils.toJSONString(new RequestResult(ResultCode.SUCCESS, resourceList));
            } else {
                error.put("dirName", "目录不存在");
            }

        } else {
            error.put("dirName", "目录名称不能为空");
        }
        return JsonUtils.toJSONString(new RequestResult(ResultCode.FAILURE, error));
    }
}
